'
' Will Strohl (will.strohl@gmail.com)
' http://www.willstrohl.com
'
'Copyright (c) 2012, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Will Strohl, Simple Redirect, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.Entities.Modules

NameSpace WillStrohl.Modules.SimpleRedirect

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' The EditSimpleRedirect class is used to manage content
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Partial Class Edit
        Inherits SimpleRedirectModuleBase

#Region " Private Members "

        Private Const CACHEKEY_PREFIX As String = "WillStrohl-SimpleRedirect-"

        Private p_EditImage As String = String.Empty
        Private p_DeleteImage As String = String.Empty

#End Region

#Region " Private Properties "

        Protected ReadOnly Property EditImage As String
            Get
                If String.IsNullOrEmpty(p_EditImage) Then
                    p_EditImage = String.Concat(Common.Globals.ApplicationPath, Icons.IconController.IconURL("Edit", "16x16"))
                End If

                Return p_EditImage
            End Get
        End Property

        Protected ReadOnly Property DeleteImage As String
            Get
                If String.IsNullOrEmpty(p_DeleteImage) Then
                    p_DeleteImage = String.Concat(Common.Globals.ApplicationPath, Icons.IconController.IconURL("Delete", "16x16"))
                End If

                Return p_DeleteImage
            End Get
        End Property

#End Region

#Region " Event Handlers "

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Page_Load runs when the control is loaded
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                If Not Page.IsPostBack Then
                    BindData()
                End If
            Catch exc As Exception    'Module failed to load
                ProcessModuleLoadException(Me, exc)
            End Try
        End Sub

        Private Sub lnkReturn_Click(sender As Object, e As System.EventArgs) Handles lnkReturn.Click
            SendBackToModule()
        End Sub

        Private Sub lnkAddRedirect_Click(sender As Object, e As System.EventArgs) Handles lnkAddRedirect.Click
            ToggleView(False)
        End Sub

        Private Sub lnkCancelRedirect_Click(sender As Object, e As System.EventArgs) Handles lnkCancelRedirect.Click
            ToggleView(True)
        End Sub

        Private Sub lnkSaveRedirect_Click(sender As Object, e As System.EventArgs) Handles lnkSaveRedirect.Click
            If Page.IsValid Then
                SaveRedirect()
            End If
        End Sub

#End Region

#Region " Helper Methods "

        Private Sub BindData()

            LocalizeModule()

            Dim strAction As String = String.Empty
            Dim strRedirect As String = String.Empty

            If Not Request.Params("action") Is Nothing Then
                strAction = Request.Params("action").ToString()
            End If

            If Not Request.Params("rid") Is Nothing Then
                strRedirect = Request.Params("rid").ToString()
            End If

            Select Case strAction
                Case "edit"
                    ToggleView(False)
                    BindForm(Integer.Parse(strRedirect, Globalization.NumberStyles.Integer))
                Case "delete"
                    DeleteRedirect(Integer.Parse(strRedirect, Globalization.NumberStyles.Integer))
                    ToggleView()
                Case Else
                    ToggleView()
            End Select

        End Sub

        Private Sub LocalizeModule()

            lnkReturn.Text = GetLocalizedString("lnkReturn.Text")
            lnkAddRedirect.Text = GetLocalizedString("lnkAddRedirect.Text")
            rfvRedirectFrom.ErrorMessage = GetLocalizedString("rfvRedirectFrom.ErrorMessage")
            rfvRedirectTo.ErrorMessage = GetLocalizedString("rfvRedirectTo.ErrorMessage")
            lnkSaveRedirect.Text = GetLocalizedString("lnkSaveRedirect.Text")
            lnkCancelRedirect.Text = GetLocalizedString("lnkCancelRedirect.Text")

        End Sub

        Private Sub SendBackToModule()

            Response.Redirect(NavigateURL())

        End Sub

        Private Sub ToggleView()
            ToggleView(True)
        End Sub

        Private Sub ToggleView(ByVal ShowGrid As Boolean)

            pnlForm.Visible = Not ShowGrid
            pnlList.Visible = ShowGrid

            Select Case ShowGrid
                Case True
                    BindGrid()
                Case Else
                    BindForm()
            End Select

        End Sub

        Private Sub BindGrid()

            Dim ctlModule As New RedirectController
            Dim collRedirect As RedirectInfoCollection = ctlModule.GetRedirects(ModuleId, True)

            If Not collRedirect Is Nothing AndAlso collRedirect.Count > 0 Then
                rptRedirect.DataSource = collRedirect
                rptRedirect.DataBind()
                rptRedirect.Visible = True
                lblNoRecords.Visible = False
            Else
                rptRedirect.Visible = False
                lblNoRecords.Visible = True
                lblNoRecords.Text = GetLocalizedString("lblNoRecords.Text")
            End If

        End Sub

        Private Sub BindForm()
            BindForm(Null.NullInteger)
        End Sub

        Private Sub BindForm(ByVal RedirectId As Integer)

            If RedirectId > 0 Then

                Dim ctlModule As New RedirectController
                Dim oRedirect As RedirectInfo = ctlModule.GetRedirect(RedirectId)

                If Not oRedirect Is Nothing Then
                    txtRedirectFrom.Text = oRedirect.RedirectFrom
                    txtRedirectTo.Text = oRedirect.RedirectTo
                    hidRedirectId.Value = oRedirect.RedirectId
                End If

            End If

        End Sub

        Private Sub SaveRedirect()

            Dim ctlModule As New RedirectController

            If Not String.IsNullOrEmpty(hidRedirectId.Value) Then
                ctlModule.UpdateRedirect(Integer.Parse(hidRedirectId.Value), ModuleId, txtRedirectFrom.Text, txtRedirectTo.Text)
            Else
                ctlModule.AddRedirect(ModuleId, txtRedirectFrom.Text, txtRedirectTo.Text)
            End If

            ' lazy invalidation of cache
            Dim collRedirect As RedirectInfoCollection = ctlModule.GetRedirects(ModuleId, True)
            DataCache.ClearCache(CACHEKEY_PREFIX)

            ClearForm()

            ToggleView(True)

        End Sub

        Private Sub ClearForm()

            txtRedirectFrom.Text = String.Empty
            txtRedirectTo.Text = String.Empty
            hidRedirectId.Value = String.Empty

        End Sub

        Private Sub DeleteRedirect(ByVal RedirectId As Integer)

            Dim ctlModule As New RedirectController
            ctlModule.DeleteRedirect(RedirectId)

        End Sub

        Protected Function ReformatUrlForEdit(ByVal RedirectId As Object) As String

            Return EditUrl(String.Empty, String.Empty, "Edit", String.Concat("&rid=", RedirectId, "&action=edit"))

        End Function

        Protected Function ReformatUrlForDelete(ByVal RedirectId As Object) As String

            Return EditUrl(String.Empty, String.Empty, "Edit", String.Concat("&rid=", RedirectId, "&action=delete"))

        End Function

#End Region

    End Class

End NameSpace